﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// ScoreHandler 的摘要说明
    /// </summary>
    public class ScoreHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.Params["type"];

            var ID = context.Request.QueryString["ID"];

            OnLineExam.BLL.Score bll = new OnLineExam.BLL.Score();

            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);
                    string depid = context.Request.Params["depid"];
                    int startIndex = (page - 1) * rows + 1;
                    int endIndex = (page) * rows;

                    //var loginuser=new UserManager().GetModel(context.User.Identity.Name);
                    var ds = bll.GetListByPage("depid=" + depid, "", startIndex, endIndex);
                    int RecordCount = bll.GetRecordCount("depid=" + depid);
                    context.Response.Write(HHG.Common.Json.DataTable2Json(ds.Tables[0], RecordCount));
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息

                    if (!string.IsNullOrEmpty(ID))
                    {
                        var m = bll.GetModel(int.Parse(ID));
                        m.ExamTime = DateTime.Now;

                        var rsult = HHG.Common.Json.GetJson<OnLineExam.Model.Score>(m);
                        context.Response.Write(rsult);
                    }

                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                    //ID = context.Request.Params["ID"];
                    //var o_model = bll.GetModel(int.Parse(ID));
                    var new_model = new OnLineExam.Model.Score();
                    //new_model.ID = int.Parse(ID);
                    new_model.DepID = int.Parse(depid);
                    new_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                     

                    

                    int nID = bll.Add(new_model);
                    context.Response.Write(nID);//修改成功，返回ID,失败，返回0

                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    ID = context.Request.Params["hdID"];
                    var o_model = bll.GetModel(int.Parse(ID));
                    //new_model = new OnLineExam.Model.JudgeProblem();
                    o_model.ID = int.Parse(ID);
                    o_model.DepID = o_model.DepID;
                    o_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    

                    if (bll.Update(o_model))
                    {
                        context.Response.Write(ID);//修改成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败，返回0
                    }
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"];
                    if (bll.Delete(int.Parse(ID)))
                    {
                        context.Response.Write(ID);//删除成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//删除失败，返回0
                    }
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}